package org.dromara.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.dromara.community.entity.AsHomeCare;
import org.dromara.community.entity.AsHomePictures;
import org.dromara.community.entity.dto.AsHomeCareDTO;
import org.dromara.community.mapper.AsHomeCareMapper;
import org.dromara.community.mapper.AsHomePicturesMapper;
import org.dromara.community.service.AsHomePicturesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AsHomePicturesServiceimpl extends ServiceImpl<AsHomePicturesMapper, AsHomePictures> implements AsHomePicturesService {


    @Autowired
    private AsHomePicturesMapper asHomePicturesMapper;


    @Autowired
    private AsHomeCareMapper asHomeCareMapper;


    @Override
    public List<AsHomeCareDTO> selectAllIds(AsHomeCare asHomeCare) {
        // 存储每个 AsHomeCareDTO 的 Map
        Map<Long, AsHomeCareDTO> asHomeCareDTOMap = new HashMap<>();

        // 获取所有 AsHomePictures
        List<AsHomePictures> asHomePicturesList = asHomePicturesMapper.selectList(null);

        // 遍历所有图片记录
        for (AsHomePictures asHomePictures : asHomePicturesList) {
            Long homecareId = asHomePictures.getHomecareId();

            // 查询 AsHomeCare 记录
            LambdaQueryWrapper<AsHomeCare> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AsHomeCare::getId, homecareId);
            AsHomeCare asHomeCareRecord = asHomeCareMapper.selectOne(wrapper);

            // 如果找到对应的 AsHomeCare 记录
            if (asHomeCareRecord != null) {
                AsHomeCareDTO asHomeCareDTO = asHomeCareDTOMap.get(homecareId);
                if (asHomeCareDTO == null) {
                    // 创建新的 DTO 对象
                    asHomeCareDTO = new AsHomeCareDTO();
                    asHomeCareDTO.setId(asHomeCareRecord.getId());
                    asHomeCareDTO.setElderlyContent(asHomeCareRecord.getElderlyContent());

                    // 初始化 URL 列表
                    asHomeCareDTO.setUrl(new ArrayList<>());

                    // 将 DTO 对象添加到 Map 中
                    asHomeCareDTOMap.put(homecareId, asHomeCareDTO);
                }

                // 添加图片 URL 到对应的 DTO 对象
                asHomeCareDTO.getUrl().add(asHomePictures.getUrl());
            }
        }

        // 返回结果列表
        return new ArrayList<>(asHomeCareDTOMap.values());
    }

}
